﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using KPIS.GERP.Models;
using KPIS.GERP.Models.Class;
using KPIS.GERP.Models.ICM;
using KPIS.GERP.Models.SYS;
using System.Data;
using System.Data.Entity;
using System.Linq.Dynamic;
using System.Web.Mvc;


namespace KPIS.GERP.WCF.GIMS
{
    [ServiceContract]
    public interface ICommitteeService
    {
        [OperationContract]
        IEnumerable<Committee> Select(Committee filter = null, List<string> includeSubObjectString = null);

        [OperationContract]
        IEnumerable<Committee> SelectByCommitteGroupId(int committeeGroupId);

        [OperationContract]
        List<SelectListItem> CommitteeSelectList(Committee criteriaModel = null);

        [OperationContract]
        int Insert(Committee model, int createByUserId);

        [OperationContract]
        void Update(Committee model, int updateByUserId);

        [OperationContract]
        void Delete(int ComId, int deleteByUserId);

        [OperationContract]
        List<Dictionary<string, string>> GetUserCommitee(int CommitteGroupId);
    }

    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "CommitteeService" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select CommitteeService.svc or CommitteeService.svc.cs at the Solution Explorer and start debugging.
    [ServiceErrorBehavior(typeof (ElmahErrorHandler))]
    public class CommitteeService : ICommitteeService
    {
        // GERPContext db = new GERPContext();      
        public IEnumerable<Committee> Select(Committee criteriaModel = null, List<string> includeSubObjectString = null)
        {
            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<Committee> query = context.Committees;

                    if (includeSubObjectString != null)
                    {
                        query = includeSubObjectString.Aggregate(query, (current, includePath) => current.Include(includePath));
                    }

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    var x = query.ToList();
                    return x;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public IEnumerable<Committee> SelectByCommitteGroupId(int committeeGroupId)
        {
            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<Committee> query = context.Committees.Include(o => o.CommitteeType).Include(o => o.CommitteePosition).Include(o => o.CommitteeFunction).Include(o => o.Employee.Position).Where(o => o.RecordStatus != "D");
                    var x = query.ToList();
                    return x;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<SelectListItem> CommitteeSelectList(Committee criteriaModel = null)
        {

            var list = new List<SelectListItem>();

            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<Committee> query = context.Committees;

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    var x = query.ToList();
                    foreach (var Committee in x)
                    {
                        var item = new SelectListItem
                            {
                                Text = Committee.Code, Value = Committee.CommitteeId.ToString()
                            };
                        list.Add(item);
                    }
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return list;
        }

        public int Insert(Committee model, int createByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    model.RecordStatus = RecordStatus.UsedStatus;
                    model.CreatedBy = createByUserId;
                    model.CreatedWhen = DateTime.Now;
                    context.Committees.Add(model);
                    context.SaveChanges();

                    return model.CommitteeId;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Update(Committee model, int updateByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    context.Entry(model).State = EntityState.Modified;
                    model.UpdatedBy = updateByUserId;
                    model.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Delete(int id, int deleteByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    var modelToDelete = context.Committees.Find(id);
                    if (modelToDelete == null) return;

                    context.Entry(modelToDelete).State = EntityState.Modified;
                    modelToDelete.RecordStatus = RecordStatus.DeletedStatus;
                    modelToDelete.UpdatedBy = deleteByUserId;
                    modelToDelete.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<Dictionary<string, string>> GetUserCommitee(int CommitteGroupId)
        {
            GERPContext context = new GERPContext();
            List<Dictionary<string, string>> Dics = new List<Dictionary<string, string>>();
            if (CommitteGroupId == 0)
            {
                Dictionary<string, string> dic = new Dictionary<string, string>();
                dic.Add("FullName", "  ");

                Dics.Add(dic);

                return Dics;
            }
            else
            {
                var x = (from a in context.Committees
                         join b in context.Employees on a.EmpId equals b.EmpId
                         join c in context.MasNamePrefixs on b.NamePrefixId equals c.NamePrefixId
                         where a.CommitteeGroupId == CommitteGroupId
                         select new { b, c }).ToList();

                foreach (var item in x)
                {
                    Dictionary<string, string> dic = new Dictionary<string, string>();
                    dic.Add("FullName", item.b.FullName);
                    
                    Dics.Add(dic);
                }

                return Dics;
            }
        }
    }
}